﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmCheckTimeEdit : Form
    {
        string _CheckID;
        CheckTimeClient _wcfCheckTime;
        CheckTimeInfo _CheckTimeInfo;
        DialogResult _dlgResult;
        string _cardNo = "";
        public frmCheckTimeEdit(string CheckID, string CardNo)
        {
            InitializeComponent();
            _dlgResult = DialogResult.Cancel;
            _wcfCheckTime = Service.CheckTime();
            _CheckTimeInfo = new CheckTimeInfo();
            _CheckID = CheckID;
            _cardNo = CardNo;
            LoadCheckList(_CheckID);
            this.Text = UpdateUI.GetString(this.Tag.ToString());
            
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        private void LoadCheckList(string CheckID)
        {
            DataTable dtCheckTime = _wcfCheckTime.CheckTime_GetInfo(CheckID).Tables[0];
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            col.Name = "Check_ID";
            col.HeaderText = "Check_ID";
            dgvList.Columns.Add(col);
            col.Visible = false;

            col = new DataGridViewTextBoxColumn();
            col.Name = "Check_CardNo";
            col.Width = 120;
            col.HeaderText = "Check_CardNo";
            dgvList.Columns.Add(col);
            col.Visible = false;


            col = new DataGridViewTextBoxColumn();
            col.Name = "Check_Date";
            col.Width = 250;
            col.HeaderText = UpdateUI.GetString("thoigianquet");
            dgvList.Columns.Add(col);

            for (int i = 0; i < dtCheckTime.Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["Check_ID"].Value = dtCheckTime.Rows[i]["Check_ID"].ToString();
                dgvList.Rows[i].Cells["Check_CardNo"].Value = dtCheckTime.Rows[i]["Check_CardNo"].ToString();
                dgvList.Rows[i].Cells["Check_Date"].Value = Convert.ToDateTime(dtCheckTime.Rows[i]["Check_Date"]).ToString("d/M/yyyy H:mm");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmCheckTimeEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = _dlgResult;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            dgvList.EndEdit();
            if (CheckValid())
            {
                CheckTimeInfo[] tracks = ReadCheckTime();

                if (tracks.Length > 0)
                {
                    for (int i = 0; i < tracks.Length; i++)
                    {
                        if (tracks[i].ID > 0)
                        {
                            if (_wcfCheckTime.CheckTime_Update(tracks[i]))
                            {
                                Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                                _dlgResult = DialogResult.OK;
                            }
                            else
                                MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            tracks[i].ID = _wcfCheckTime.CheckTime_Insert(tracks[i]);
                            if (tracks[i].ID > 0)
                            {
                                dgvList.Rows[i].Cells["Check_ID"].Value = tracks[i].ID;                            
                                Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                                _dlgResult = DialogResult.OK;
                            }
                            else
                                MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int _checkID = 0;
            if (dgvList.SelectedRows.Count > 0)
            {
                _checkID = Convert.ToInt32(dgvList.SelectedRows[0].Cells["Check_ID"].Value);
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfCheckTime.CheckTime_Delete(_checkID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        _dlgResult = DialogResult.OK;
                        dgvList.Rows.Remove(dgvList.SelectedRows[0]);
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            int n = dgvList.Rows.Count;
            if (n == 0)
                dgvList.Rows.Add();
            else
                dgvList.Rows.Insert(n, 1);

            dgvList.Rows[n].Cells["Check_ID"].Value = 0;
            dgvList.Rows[n].Cells["Check_CardNo"].Value = _cardNo;// dgvList.Rows[n - 1].Cells["Check_CardNo"].Value;
            dgvList.Rows[n].Cells["Check_Date"].Value = "";

        }

        bool CheckValid()
        {
            DateTime _DateVal;
            int n = dgvList.Rows.Count;
            for (int i = 0; i < n; i++)
            {
                if (!DateTime.TryParseExact(dgvList.Rows[i].Cells["Check_Date"].Value.ToString(), "d/M/yyyy H:m", null, System.Globalization.DateTimeStyles.None, out _DateVal))
                {
                    MessageBox.Show(UpdateUI.GetString("thongbaodinhdangsai"), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    dgvList.Rows[i].Cells["Check_Date"].Selected = true;
                    return false; ;
                }

            }
            return true;
        }

        private CheckTimeInfo[] ReadCheckTime()
        {
            int n = dgvList.Rows.Count;
            CheckTimeInfo[] tracks = new CheckTimeInfo[n];
            for (int i = 0; i < n; i++)
            {
                DateTime outDate;
                tracks[i] = new CheckTimeInfo();
                tracks[i].ID = Convert.ToInt32(dgvList.Rows[i].Cells["Check_ID"].Value);
                if (DateTime.TryParseExact(dgvList.Rows[i].Cells["Check_Date"].Value.ToString(), "d/M/yyyy H:m", null, System.Globalization.DateTimeStyles.None, out outDate))
                    tracks[i].Date = outDate;
                tracks[i].IsInsert = false;
                tracks[i].Status = "";
                tracks[i].CardNo = dgvList.Rows[i].Cells["Check_CardNo"].Value.ToString();
            }
            return tracks;
        }
    }
}
